package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;

public class ControllerTow  implements Initializable {

    private SimpleTools simpleTools = new SimpleTools();

    @FXML
    private Stage stage;

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    @FXML
    private TextField tf_appid;
    @FXML
    private TextField tf_appkey;
    @FXML
    private TextField tf_bundleid;
    @FXML
    private TextField tf_version;
    @FXML
    private TextField tf_platform;
    @FXML
    private TextField tf_inputSymbol;
    @FXML
    private Button bt_inputSymbol;
    @FXML
    private TextField tf_inputMapping;
    @FXML
    private Button bt_inputMapping;
    @FXML
    private Button bt_commit;

    // 界面打开后的初始化操作
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // 可在此给各控件设置单击事件或者选中事件，也可设置控件上的文本字体及其大小

//        //提交按钮
//        bt_commit.setOnAction(e -> {
//
//            String appid = tf_appid.getText().trim();
//            String appkey = tf_appkey.getText().trim();
//            String bundleid = tf_bundleid.getText().trim();
//            String version = tf_version.getText().trim();
//            String platform = tf_platform.getText().trim();
//            String inputSymbol = tf_inputSymbol.getText().trim();
//            String inputMapping = tf_inputMapping.getText().trim();
//
//            //如果有数据为null就直接弹窗显示，不做处理
//            if (checkConfigNull(appid, appkey, bundleid, version, platform, inputSymbol, inputMapping)) {
//                return;
//            }
//
//            String commandStr = "java -jar buglyqq-upload-symbol.jar -appid %s -appkey %s -bundleid %s -version %s -platform %s -inputSymbol %s -inputMapping %s";
//            commandStr = String.format(commandStr, appid, appkey, bundleid, version, platform, inputSymbol, inputMapping);
//
//            String finalCommandStr = commandStr;
//
//            new Thread(() -> {
//                try {
//                    ProcessBuilder pb = new ProcessBuilder("cmd", "/k", "start " + finalCommandStr);
//
//                    //切换工作目录 （ 很重要，如果需要切到其它盘执行某些命令的话 ）
//                    //这里的目录地址一定是您下载腾讯bugly的工具jar地址
//                    pb.directory(new File("D://Programe File//buglyqq-upload-symbol"));
//                    Process process = pb.start();
//                    //打印数据
//                    byte[] bytePool = new byte[1024];
//                    StringBuffer stringBuffer = new StringBuffer();
//                    String line;
//                    InputStream inputStream = (process.getInputStream());
//                    while (inputStream.read(bytePool) != -11) {
//                        line = new String(bytePool, "UTF-8");
//                        System.out.println("取出的数据为：" + line);
//                        stringBuffer.append(line);
//                        stringBuffer.append("\n");
//                    }
//                    inputStream.close();
//                    //关闭进程
//                    process.destroy();
//
//                } catch (Exception es) {
//
//                }
//            }).run();
//        });
    }

    private boolean checkConfigNull(String appid, String appkey, String bundleid, String version, String platform, String inputSymbol, String inputMapping) {


        boolean isconfigNull ;
        isconfigNull = false;
        if ("".equals(appid)) {
            simpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "appid不能未空");
            isconfigNull = true;
        }
        if ("".equals(appkey)) {
            simpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "appkey不能未空");
            isconfigNull = true;
        }
        if ("".equals(bundleid)) {
            simpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "bundleid（包名）不能未空");
            isconfigNull = true;
        }
        if ("".equals(version)) {
            simpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "version不能未空");
            isconfigNull = true;
        }
        if ("".equals(platform)) {
            simpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "平台不能未空");
            isconfigNull = true;
        }
        if ("".equals(inputSymbol)) {
            simpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "Mapping文件根路径不能未空");
            isconfigNull = true;
        }
        if ("".equals(inputMapping)) {
            simpleTools.informationDialog(Alert.AlertType.WARNING, "提示", "警告", "Mapping文件路径不能未空");
            isconfigNull = true;
        }
        return isconfigNull;
    }
}
